﻿namespace LMT
{
    using Microsoft.Win32;
    using System.Collections.Generic;

    /// <summary>
    /// 
    /// </summary>
    public abstract class Registry
    {
        /// <summary>
        /// 
        /// </summary>
        public const string WINDOW_RUN = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

        /// <summary>
        /// get a registry key for list program will be call when window is start up.
        /// </summary>
        /// <param name="keyPermission">value indicate permission will action on key</param>
        /// <returns>RegistryKey</returns>
        /// <exception cref="System.SecurityException"></exception>
        internal static RegistryKey GetWindowAutoRunApp(RegistryKeyPermissionCheck keyPermission)
        {
            Microsoft.Win32.RegistryKey registKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(WINDOW_RUN, keyPermission);
            return registKey;
        }

        /// <summary>
        /// Get array string contains all program will be call when window start up.
        /// </summary>
        /// <returns>Dictionary{string,string} : first string is appKey to indicate program, second is full path of application.</returns>
        /// <exception cref="System.SecurityException"></exception>
        public static Dictionary<string,string> AutoRunList()
        {
            Dictionary<string, string> listApp = null;
            RegistryKey startupKey = Registry.GetWindowAutoRunApp(RegistryKeyPermissionCheck.ReadSubTree);

            if (startupKey != null)
            {
                // Get all value key
                string[] arrayApp = startupKey.GetValueNames();

                // Assign each key into collection
                listApp = new Dictionary<string, string>(5);
                foreach (string appKey in arrayApp)
                    listApp.Add(appKey, 
                        startupKey.GetValue(appKey, string.Empty, RegistryValueOptions.None).ToString());

                // Close key for other app
                startupKey.Close();

                return listApp;
            }

            return null;
        }

        /// <summary>
        /// set a program file will be call when Window is start up.
        /// </summary>
        /// <param name="keyName">a key name help window indicate a app</param>
        /// <param name="appPath">full path of program file, include startup parameter</param>
        /// <example>SetStartup("Avp", "C:\Program Files\App\app.exe -s")</example>
        /// <exception cref="System.UnauthorizedAccessException"></exception>
        /// <exception cref="System.SecurityException"></exception>
        public static void SetStartup(string keyName, string appPath)
        {
            RegistryKey startupKey = Registry.GetWindowAutoRunApp(RegistryKeyPermissionCheck.ReadWriteSubTree);

            if (startupKey != null)
            {
                try
                {
                    // Try to set value
                    startupKey.SetValue(keyName, appPath, RegistryValueKind.String);
                }
                finally
                {
                    if (startupKey != null)
                        startupKey.Close();
                }
            }
        }
        
    }
}
